package com.exam.mapper;

import com.exam.entity.Questionnaire;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;


import java.util.List;

@Mapper
public interface QuestionnaireMapper {


    /**
     * 查询所有问卷，问卷列表
     * @return
     */
    @Select("SELECT * FROM questionnaire_info where question_stop != 4 and question_stop != 5;")
    List<Questionnaire> queryAll();

    /**
     * 通过id删除问卷，其实是放到了回收站
     * @return
     */
    @Update("UPDATE questionnaire_info SET question_stop = 4 WHERE id = #{id} and question_stop != 2")
    int deleteQuestionnaireById(String id);

    /**
     * 通过id从回收站恢复问卷
     * @return
     */
    @Update("UPDATE questionnaire_info SET question_stop = 3 WHERE id = #{id} ")
    int recycleQuestionnaireById(String id);

    /**
     * 模糊搜索问卷
     * @param questionName
     * @return
     */
    @Select("Select * FROM questionnaire_info where question_name like concat('%','${questionName}','%') and question_stop !=4 ;")
    List<Questionnaire> fuzzySearchQuestionnaire(Questionnaire questionName);
    /**
     * 查询所有回收问卷
     * @return
     */
    @Select("SELECT * FROM questionnaire_info where question_stop = 4;")
    List<Questionnaire> queryRecycleAll();
    /**
     * 通过id从回收站彻底删除问卷
     * @return
     */
    @Update("UPDATE questionnaire_info SET question_stop = 5 WHERE id = #{id}")
    int finallyDeleteById(String id);
    /**
     * 新增问卷
     * @return
     */
    @Insert("insert into questionnaire_info(id,question_name,question_stop,answerTime,answer_total,question_content,status) " +
            "values(#{id},#{questionName},#{questionStop} , #{answerTime}, #{answerTotal}, #{questionContent}, #{status})")
    int addQuestionnaire(Questionnaire questionnaire);
    /**
     * 更改问卷状态为发布
     * @return
     */
    @Update("UPDATE questionnaire_info SET question_stop = 2 WHERE id = #{id} and question_stop != 4")
    int sendQuestionnaire(String id);

    /**
     * 更改问卷状态为停止
     * @return
     */
    @Update("UPDATE questionnaire_info SET question_stop = 3 WHERE id = #{id} and question_stop != 4")
    int stopQuestionnaire(String id);

    @Select("Select * FROM questionnaire_info where id = #{id};")
    Questionnaire findQuestionnaireById(String id);

    @Update("UPDATE questionnaire_info SET answer_total = #{answerTotal} WHERE id = #{id} ")
    void reduceNum(Questionnaire questionnaire);
}
